package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.ysd.dao.impl.BannerDaoImpl;
import com.ysd.entity.Banner;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminBannerServlet extends BaseServlet {
	/**
	 * 查询所有的轮播图分类 localhost:8080/meizizi/adminBanner?reqName=getBanner
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getBanner(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		BannerDaoImpl baDao = new BannerDaoImpl();// 创建实现类对象
		List<Banner> baList = baDao.getBanner();// 查到的所有轮播图集合

		// 发送ajax请求服务器基本会返回json格式字符串
		response.setCharacterEncoding("UTF-8");// 设置响应的编码格式
		PrintWriter out = response.getWriter(); // out对象

		Gson gson = new Gson();
		String res = gson.toJson(baList);// 将baList转化为json字符串
		out.print(res);// 响应数据
		out.flush();
		out.close();

	}

	/**
	 * localhost:8080/meizizi/adminBanner?reqName=deleteBanner
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void deleteBanner(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");// 设置响应的编码格式
		PrintWriter out = response.getWriter();// out对象
		int cId = Integer.parseInt(request.getParameter("bannerId"));// 接收前台传来的参数
		BannerDaoImpl baDao = new BannerDaoImpl();
		int n = baDao.deleteBanner(cId);// 调用删除方法
		if (n > 0) {// 删除成功
			out.print(Result.toClient(true, "删除成功", ""));// {"success":true,"message":"删除成功","remark":""}
		} else {// 删除失败
			out.print(Result.toClient(false, "删除失败", ""));// {"success":false,"message":"删除失败","remark":""}
		}

		out.flush();
		out.close();
	}

	/**
	 * localhost:8080/meizizi/adminBanner?reqName=addBanner
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addBanner(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");// 设置编码格式
		PrintWriter out = response.getWriter();

		BannerDaoImpl baDao = new BannerDaoImpl();// 轮播图的实现对象

		String bannerImg = request.getParameter("bannerImg");// 接收前台传来的参数
		if (bannerImg != null && !bannerImg.equals("")) {
			List<Banner> baList = baDao.BannerImg(bannerImg);
			if (baList.size() == 0) {// 没有查到
				Banner ba = new Banner(0, bannerImg);// 根据接收的参数创建轮播图对象
				int n = baDao.addBanner(ba);
				if (n > 0) {
					out.print(Result.toClient(true, "添加成功", ""));
				} else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
			} else {// 查到了，不能添加
				out.print(Result.toClient(false, "该图片以存在", ""));
			}
		}

		out.flush();// 清空
		out.close();// 关闭
	}

	/**
	 * localhost:8080/meizizi/adminBanner?reqName=addUpload
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addUpload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();

		// 1.创建SmarUpload对象
		SmartUpload smart = new SmartUpload();
		// 2.设置编码格式
		smart.setCharset("UTF-8");
		// 3.初始化
		smart.initialize(getServletConfig(), request, response);  

		try {// 正常
				// 4.上传
			smart.upload();
			// 5.设置上传文件名
			String fileName = System.currentTimeMillis() + ".jpg";
			// 6.将文件保存到指定目录
			smart.getFiles().getFile(0).saveAs("/images/" + fileName);
			// 7.响应数据
			out.print(Result.toClient(true, "上传成功", fileName));
		} catch (SmartUploadException e) {// 异常
			e.printStackTrace();
			out.print(Result.toClient(false, "上传失败", "图片过大"));
		}
		out.flush();
		out.close();
	}
	/**
	 * localhost:8080/meizizi/ adminBanner?reqName=updateBannerId
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getBannerId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		BannerDaoImpl baDao = new BannerDaoImpl();
		int id = Integer.parseInt(request.getParameter("cId"));
		Banner  b =	baDao.getBannerId(id);
		Gson gson =new Gson();
		String res = gson.toJson(b);
		out.print(res);
		
		
		out.flush();
		out.close();
	}
	/**
	 * localhost:8080/meizizi/ adminBanner?reqName=updateBannerId
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateBannerId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		BannerDaoImpl baDao = new BannerDaoImpl();
		int bannerId = Integer.parseInt(request.getParameter("bannerId"));
		String bannerImg = request.getParameter("bannerImg");
		Banner ban = new Banner(bannerId, bannerImg);
		List<Banner> bannList =	baDao.BannerImg(bannerImg);
		if(bannList.size()==0) {
			int n =	baDao.updateBanner(ban);
			if(n>0){
				out.print(Result.toClient(true, "修改成功",""));
			}else {
				out.print(Result.toClient(false, "修改失败",""));
			}	

		}else {
			out.print(Result.toClient(false, "轮播图已存在",""));
		}
		
		
		
		out.flush();
		out.close();
	}
	/**
	 * localhost:8080/meizizi/adminBanner?reqName=delBannerByIds
	 * 根据id批量删除轮播图
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delBannerByIds(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");// 设置响应的编码格式
		PrintWriter out = response.getWriter();// out对象
		String ids=request.getParameter("ids");// 接收前台传来的参数
		BannerDaoImpl baDao = new BannerDaoImpl();
		int n = baDao.delBannerByIds(ids);// 调用删除方法
		if (n > 0) {// 删除成功
			out.print(Result.toClient(true, "删除成功", ""));// {"success":true,"message":"删除成功","remark":""}
		} else {// 删除失败
			out.print(Result.toClient(false, "删除失败", ""));// {"success":false,"message":"删除失败","remark":""}
		}

		out.flush();
		out.close();
	}
}
